package main

import (
	"fmt"
	"slices"
)

func threeSum(nums []int) [][]int {
	slices.Sort(nums)
	var res [][]int
	for k := 0; k < len(nums) && nums[k] <= 0; k++ {
		if k > 0 && nums[k] == nums[k-1] {
			continue
		}
		for i, j := k+1, len(nums)-1; i < j; {
			sum := nums[k] + nums[i] + nums[j]
			if sum < 0 {
				i++
				for i < j && nums[i] == nums[i-1] {
					i++
				}
			} else if sum > 0 {
				j--
				for i < j && nums[j] == nums[j+1] {
					j--
				}
			} else {
				res = append(res, []int{nums[k], nums[i], nums[j]})
				i++
				j--
				for i < j && nums[i] == nums[i-1] {
					i++
				}
				for i < j && nums[j] == nums[j+1] {
					j--
				}
			}
		}
	}
	return res
}

func main() {
	nums := []int{-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0}
	sum := threeSum(nums)
	fmt.Println(sum)
}
